package com.chinaunicom.emergency.service;

import com.chinaunicom.emergency.action.TokenExtendAction;
import com.chinaunicom.emergency.model.Logs;
import com.chinaunicom.emergency.repository.LogsRepository;
import com.chinaunicom.emergency.util.JPAUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @Author liliang
 * @Date 2019/12/17 15:37
 * @Version 1.0
 */
@Service
@Log4j2
public class LogsService {
    @Autowired
    private LogsRepository logsRepository;
    @Autowired
    private TokenExtendAction authClient;


    public Specification findPageByCondition(Map para){
        return new Specification<Logs>() {

            @Override
            public Predicate toPredicate(Root<Logs> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();

                JPAUtil.like(list,cb,root,"logDescribe",para);
                JPAUtil.equal(list,cb,root,"logType",para);
                JPAUtil.equal(list,cb,root,"createBy",para);
                JPAUtil.equal(list,cb,root,"logType",para);
                JPAUtil.greaterThanOrEqualToDate(list,cb,root,"createTime",para,"startDate");
                JPAUtil.lessThanOrEqualToDate(list,cb,root,"createTime",para, "endDate");
                return cb.and(list.toArray(new Predicate[list.size()]));
            }
        };
    }

}
